/*
 * @Author: lizilong 94648929@qq.com
 * @Date: 2022-12-21 18:35:13
 * @LastEditors: lizilong 94648929@qq.com
 * @LastEditTime: 2024-03-06 20:26:00
 * @FilePath: \electron-app\src\router\index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { createRouter, createWebHistory } from "vue-router";
import {
  staticRouter,
  errorRouter,
  asyncRouter,
  notFoundRouter,
} from "@/router/staticRouter";
import { ElNotification } from "element-plus";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [...staticRouter, ...asyncRouter, ...errorRouter, notFoundRouter],
});

// 全局路由守卫
/**
 * @description 路由拦截 beforeEach
 * */
router.beforeEach(async (to: any, _from, next) => {
  const token = "123";
  // 2.设置页面
  document.title = to.meta.title;
  // 3.如果是开放页面，直接放行
  if (to.meta.open || token) return next();

  next("/account/login");
});

/**
 * @description 路由跳转结束
 * */
// router.afterEach(() => {});

/**
 * @description 路由跳转错误
 * */
router.onError((error) => {
  ElNotification.error({ title: "路由错误", message: error.message });
});

export default router;
